package com.example.springbook.book.service;

import com.example.springbook.book.constant.Constants;
import com.example.springbook.book.mapper.UserMapper;
import com.example.springbook.book.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public Boolean checkUserAndPassword(String userName, String password, HttpSession session) {
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userMapper.selectUser(userName);
        if (userInfo == null) {
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            //账号密码正确
            //Session的内容取决于后面需要从session中获取什么
//            session.setAttribute("userName", userName);
            session.setAttribute(Constants.SESSION_USER_KEY,userName);
            return true;
        }
        return false;
    }
}
